package com.buaa.dateandtimepicker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements DatePicker.OnDateChangedListener,TimePicker.OnTimeChangedListener {
    private DatePicker datePicker;
    private TimePicker timePicker;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datePicker=(DatePicker)findViewById(R.id.dataPicker);
        timePicker=(TimePicker)findViewById(R.id.timePicker);
        textView=(TextView)findViewById(R.id.textview);
        datePicker.init(2001,1,25,this);
        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(this);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar=Calendar.getInstance();
        calendar.set(datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth(),timePicker.getCurrentHour(),timePicker.getCurrentMinute());
        SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
        textView.setText(format.format(calendar.getTime()));
    }

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(MainActivity.this,hourOfDay+minute+"",Toast.LENGTH_SHORT).show();
    }
}
